package net.zjitc.service.impl;

import net.zjitc.common.ServiceResponse;
import net.zjitc.mapper.ProductsMapper;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Condition;
import net.zjitc.pojo.Products;
import net.zjitc.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author jinliwei
 * @Date 2019/4/2 23:55
 **/
@Service
@Transactional
public class ProductsServiceImpl implements ProductsService {
    @Autowired
    ProductsMapper productsMapper;
    @Override
    public List<Products> findAllProducts() {
        return productsMapper.findAllProducts();
    }

    @Override
    public ServiceResponse addProduct(Products products, MultipartFile file, HttpServletRequest request) {
        //数据库存的是什么？路径
        //文件存在哪里？磁盘（一定不在数据库中）当前项目下

        String path = request.getServletContext().getRealPath("/product/");
        if (file != null){
            //文件存的目录，如果不存在则要创建此目录
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdirs();
            }
            //文件名称
            String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
            //创建目录下的文件
            File filePath = new File(dir, fileName);
            try {
                //将上传的文件写入目录文件(本地)
                file.transferTo(filePath);
            } catch (IOException e) {
                e.printStackTrace();
                return ServiceResponse.createByErrorMessage("写入失败");
            }
            //为products设置文件存储的路径，以便于在于数据库中
            products.setImgurl("/product/"+fileName);
        }
        products.setId(UUID.randomUUID().toString());
        int i = productsMapper.addProduct(products);
        if (i > 0){
            return ServiceResponse.createBySuccessAndMessage("添加成功");
        }
        return ServiceResponse.createByErrorMessage("添加失败");
    }

    @Override
    public List<Category> findAllCategory() {
        return productsMapper.findAllCategory();
    }

    @Override
    public Products findProductById(String id) {
        return productsMapper.findProductById(id);
    }

    @Override
    public ServiceResponse editProduct(Products products, MultipartFile file, HttpServletRequest request) {
        int i = productsMapper.editProduct(products);
        if (i > 0){
            return ServiceResponse.createBySuccessAndMessage("修改成功");
        }
        return ServiceResponse.createByErrorMessage("修改失败");
    }

    @Override
    public int deleteProduct(String id) {
        return productsMapper.deleteProduct(id);
    }

    @Override
    public List<Products> searchByCondition(Condition condition) {
        return productsMapper.searchByCondition(condition);
    }

    @Override
    public Products findNewProducts(String product_id) {
        return productsMapper.findNewProducts(product_id);
    }

    @Override
    public Category findCategoryById(Integer id) {
        if (id==null){
            Category category = new Category();
            category.setName("全部商品目录");
            return category;
        }else {
            return productsMapper.findCategoryById(id);
        }
    }

    @Override
    public List<Products> findProductByCid(Integer id) {
        if (id==null){
            return productsMapper.findAllProducts();
        }
        return productsMapper.findProductByCid(id);
    }


}
